#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
import threading
import time
from queue import Queue


class MultiThread:
    pre_queue = Queue()
    post_queue = Queue()
    active = True

    # def pre_operation(self):
    #     print(f'进程:{threading.current_thread().name} 正在运行')
    #     while self.active:
    #         if not self.pre_queue.empty():
    #             value = self.pre_queue.get()
    #             print(f'正在预处理:{str(value)}')
    #             self.post_queue.put(value)
    #         time.sleep(1)
    #
    # def pos_operation(self):
    #     print(f'进程:{threading.current_thread().name} 正在运行')
    #     while self.active:
    #         if not self.post_queue.empty():
    #             value = self.post_queue.get()
    #             print(f'正在后续处理:{str(value)}')
    #             self.post_queue.task_done()
    #
    #         time.sleep(2)

    def test_put(self):
        for i in range(3):
            self.pre_queue.put(i)
            time.sleep(5)

    def test_get(self):
        for i in range(3):
            print(self.pre_queue.get(timeout=1))
            self.pre_queue.task_done()


if __name__ == '__main__':
    multi = MultiThread()
    # put_thread = threading.Thread(target=multi.test_put)
    # get_thread = threading.Thread(target=multi.test_get)
    multi.pre_queue.put(1)
    print('第一个元素获取')
    print(multi.pre_queue.get())
    print('第二个元素获取')
    print(multi.pre_queue.get())

    # pre_thread = threading.Thread(target=multi.pre_operation)
    # post_thread = threading.Thread(target=multi.pos_operation)
    # pre_thread.start()
    # post_thread.start()
    #
    # print(f'进程:{threading.current_thread().name} 正在运行')
    # for i in range(5):
    #     multi.pre_queue.put(i)
    #
    # multi.post_queue.join()
    # # if multi.post_queue.empty():
    # #     multi.active = False
    #
    # pre_thread.join()
    # post_thread.join()
    # print('结束')
